/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sony
 */
public class UserData {

    public static ArrayList<User> getAllUser() throws ClassNotFoundException, SQLException {
        ArrayList<User> listuser = new ArrayList<User>();
        Connection cn = DataAcess.ConnectionDatabase();
        try {
            String sql = "select * from Users";
            PreparedStatement ps = cn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setFname(rs.getString("fname"));
                user.setLname(rs.getString("lname"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setCity(rs.getString("city"));
                user.setPhone(rs.getInt("phone"));
                user.setGender(rs.getString("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setJob(rs.getString("job"));
                user.setRole(rs.getString("role"));
                listuser.add(user);
            }
            return listuser;
        } catch (SQLException ex) {
            Logger.getLogger(UserData.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            cn.close();
        }

    }

    public static User getUserByID(int id) throws ClassNotFoundException, SQLException {
        Connection cn = DataAcess.ConnectionDatabase();
        try {
            String sql = "select * from Users where id = ?";
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setFname(rs.getString("fname"));
                user.setLname(rs.getString("lname"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setCity(rs.getString("city"));
                user.setPhone(rs.getInt("phone"));
                user.setGender(rs.getString("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setJob(rs.getString("job"));
                user.setRole(rs.getString("role"));
                return user;
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(UserData.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            cn.close();
        }
    }

    public static ArrayList<User> searchUser(String keyseach) throws ClassNotFoundException, SQLException {
        ArrayList<User> listuser = new ArrayList<User>();
        Connection cn = DataAcess.ConnectionDatabase();
        try {
            String sql = "select * from Users where email like ?";
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setString(1, "%" + keyseach + "%");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setFname(rs.getString("fname"));
                user.setLname(rs.getString("lname"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setCity(rs.getString("city"));
                user.setPhone(rs.getInt("phone"));
                user.setGender(rs.getString("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setJob(rs.getString("job"));
                user.setRole(rs.getString("role"));
                listuser.add(user);
            }
            return listuser;
        } catch (SQLException ex) {
            Logger.getLogger(UserData.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            cn.close();
        }
    }

    public static User authenticateUser(String email, String password) throws ClassNotFoundException, SQLException {
        Connection cn = DataAcess.ConnectionDatabase();
        try {
            String sql = "select * from Users where email = ? and password = ?";
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setString(1, email);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setFname(rs.getString("fname"));
                user.setLname(rs.getString("lname"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setAddress(rs.getString("address"));
                user.setCity(rs.getString("city"));
                user.setPhone(rs.getInt("phone"));
                user.setGender(rs.getString("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setJob(rs.getString("job"));
                user.setRole(rs.getString("role"));

                return user;
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(UserData.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            cn.close();
        }
    }

    public static String getRole(String email) throws ClassNotFoundException, SQLException {
        Connection cn = DataAcess.ConnectionDatabase();
        try {
            String sql = "select role from Users where email = '" + email + "'";
            PreparedStatement ps = cn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                return rs.getString("role");
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(UserData.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            cn.close();
        }
    }

    public static boolean insertUser(User user) throws ClassNotFoundException, SQLException {

        try {
            Connection cn = DataAcess.ConnectionDatabase();
            String sql = "insert into Users(fname, lname, password, email, address, city, phone, gender, birthday, job, role) values (?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setString(1, user.getFname());
            ps.setString(2, user.getLname());
            ps.setString(3, user.getPassword());
            ps.setString(4, user.getEmail());
            ps.setString(5, user.getAddress());
            ps.setString(6, user.getCity());
            ps.setInt(7, user.getPhone());
            ps.setString(8, user.getGender());
            ps.setDate(9, user.getBirthday());
            ps.setString(10, user.getJob());
            ps.setString(11, user.getRole());
            int rs = ps.executeUpdate();
            cn.close();
            return rs > 0;
        } catch (SQLException ex) {
            Logger.getLogger(UserData.class.getName()).log(Level.SEVERE, null, ex);
            throw new SQLException();
        }
    }
}
